package com.it.tydic.estate.module.service.impl;

import com.it.tydic.estate.common.util.CurrentUtil;
import com.it.tydic.estate.common.util.InstaIdGenerator;
import com.it.tydic.estate.common.util.TimeUtil;
import com.it.tydic.estate.module.mapper.inner.TbResourceMapper;
import com.it.tydic.estate.module.mapper.inner.TbRoleResourceMapper;
import com.it.tydic.estate.module.model.inner.TbResource;
import com.it.tydic.estate.module.model.inner.TbRole;
import com.it.tydic.estate.module.model.inner.TbRoleResource;
import com.it.tydic.estate.module.model.inner.TbUser;
import com.it.tydic.estate.module.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.*;

@Service
@Transactional
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private TbResourceMapper tbResourceMapper;

    @Autowired
    private TbRoleResourceMapper  tbRoleResourceMapper;

    @Override
    public int deleteById(String id) {
        return tbResourceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public TbResource findById(String id) {
        return tbResourceMapper.selectByPrimaryKey(id);
    }


    @Override
    public int deleteByIds(List<String> idList) {
        Example example = new Example(TbUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("resourceId", idList);
        return tbResourceMapper.deleteByExample(example);
    }

    @Override
    public int addOrUpdate(TbResource tbResource) {
        int index=0;
        String userId= CurrentUtil.getCurrentUserId();
        if(StringUtil.isEmpty(tbResource.getResourceId())){
            int count=countResourceNameByName(tbResource.getResourceName());
            if(count > 0){
                return -1;
            }
            InstaIdGenerator idGenerator = new InstaIdGenerator();
            tbResource.setResourceId(idGenerator.nextId(TimeUtil.timeGen())+"");
            tbResource.setCreateTime(new Date());
            tbResource.setCreateUser(userId);
            tbResource.setStatus(0);
            if(tbResource.getResourceOrder() == null){
                tbResource.setResourceOrder(0);
            }
            index= tbResourceMapper.insert(tbResource);
        }else{
            tbResource.setUpdateTime(new Date());
            tbResource.setUpdateUser(userId);
            index=  tbResourceMapper.updateByPrimaryKeySelective(tbResource);
        }
        return index;
    }

    private int countResourceNameByName(String resourceName) {
        Example example = new Example(TbResource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("resourceName", resourceName);
        return  tbResourceMapper.selectCountByExample(example);
    }

    @Override
    public List<TbResource> findResourceListByRoleId(String roleId) {
        //根据角色查询资源菜单id
        Example example = new Example(TbRoleResource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleId",roleId);
        List<TbRoleResource> tbRoleResources=tbRoleResourceMapper.selectByExample(example);
        List<String> resouceIds=new ArrayList<>();
        for(TbRoleResource tbRoleResource : tbRoleResources){
            resouceIds.add(tbRoleResource.getResourceId());
        }
        //查询菜单
        if(resouceIds.size() > 0){
            Example resouceExample = new Example(TbResource.class);
            resouceExample.setOrderByClause("RESOURCE_ORDER ASC");
            Example.Criteria resourceCriteria = resouceExample.createCriteria();
            resourceCriteria.andIn("resourceId",resouceIds);
           List<TbResource> resources= tbResourceMapper.selectByExample(resouceExample);
            if(resources.size() > 0){
                return resources;
            }
        }
        return Collections.emptyList();
    }
    @Override
    public List<TbResource> findResourceTreeListByRoleId(String roleId) {
        List<TbResource> resources=findResourceListByRoleId(roleId);
        if(resources.size() > 0){
            return toTree(resources);
        }
        return Collections.emptyList();
    }

    @Override
    public boolean resourceIsUseByList(List<String> idList) {
        Example example = new Example(TbRoleResource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("resourceId", idList);
        int count=countResourceByExample(example);
        if(count > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean resourceIsUseById(String id) {
        Example example = new Example(TbRoleResource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("resourceId", id);
        int count=countResourceByExample(example);
        if(count > 0){
            return true;
        }
        return false;
    }
    public int countResourceByExample(Example example){
        return tbRoleResourceMapper.selectCountByExample(example);
    }
    @Override
    public List<TbResource> initMenu() {
        return toTree( tbResourceMapper.selectAll());
    }

    private List<TbResource> toTree(List<TbResource> menu) {
        Map<String, TbResource> temp = new LinkedHashMap<>();
        for (TbResource r : menu) {
            temp.put(r.getResourceId(), r);
        }
        List<TbResource> result = new ArrayList<TbResource>();
        for (TbResource child : menu) {
            String parentId = child.getParentId();
            if (parentId.equals(String.valueOf(0))) {
                result.add(child);
                continue;
            }
            TbResource parent = temp.get(parentId);
            if(parent != null){
                parent.getChildren().add(child);
            }
        }
        return result;
    }
}
